package com.lagou.jdbc01;

import java.sql.*;

public class JDBCDemo03 {
    public static void main(String[] args) {
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "15061129384poi..");
            //3.获取语句执行的对象
            statement=connection.createStatement();
            //4.执行SQL
            String sql="select * from jdbc_user;";
            resultSet=statement.executeQuery(sql);
            //5.获取id
            while(resultSet.next()){
                int i=resultSet.getInt("id");
                System.out.println("通过列名获取到的:"+i);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //finally中的代码始终会执行
            //5.处理结果集对象
                try {
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
    }
}
